package buildings.dwelling;

import buildings.exceptions.InvalidRoomsCountException;
import buildings.exceptions.InvalidSpaceAreaException;
import buildings.Space;
import java.io.Serializable;

public class Flat implements Space, Serializable, Cloneable {

    private float flatSquare;
    private int roomCount;
    private final float DEFAULT_FLAT_SQUARE = 50;
    private final int DEFAULT_ROOM_COUNT = 2;

    public Flat() {
        this.flatSquare = DEFAULT_FLAT_SQUARE;
        this.roomCount = DEFAULT_ROOM_COUNT;
    }

    public Flat(float flatSquare) {
        this.flatSquare = flatSquare;
        this.roomCount = DEFAULT_ROOM_COUNT;
    }

    public Flat(int roomCount, float flatSquare) {
        if (flatSquare <= 0) {
            throw new InvalidSpaceAreaException();
        }
        if (roomCount <= 0) {
            throw new InvalidRoomsCountException();
        }
        this.flatSquare = flatSquare;
        this.roomCount = roomCount;
    }

    public int getRoomCount() {
        return roomCount;
    }

    public float getSquare() {
        return flatSquare;
    }

    public void setRoomCount(int roomCount) {
        if(roomCount <= 0) {
            throw new InvalidRoomsCountException();
        }
        this.roomCount = roomCount;
    }

    public void setSquare(float flatSquare) {
        if (flatSquare <= 0) {
            throw new InvalidSpaceAreaException();
        }
        this.flatSquare = flatSquare;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer(this.getClass().getSimpleName() + 
                " (" + roomCount + ", " + flatSquare + ")");
        return buf.toString();
    }

    public boolean equals(Object obj) {
        return ((obj instanceof Flat) 
                && ((Flat)obj).getSquare() == flatSquare) 
                && (((Flat)obj).getRoomCount() == roomCount);
    }

    public int hashCode() {
        return roomCount | Float.floatToIntBits(flatSquare);
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    
}
